package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.SetmealDao;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.result.PageResult;
import com.itheima.health.result.QueryPageBean;
import com.itheima.health.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealDao setmealDao;
    /*
    * 新增套餐
    * */
    @Transactional
    @Override
    public void Addsetmeal(Setmeal setmeal, List<String> checkgroupIds) {
        if (setmeal !=null){
            //添加套餐
            setmealDao.addSetmeal(setmeal);
        }
        //获取套餐id
        Integer setmealid = setmeal.getId();
        //在添加套餐关联的分组id
        for (String checkGroup : checkgroupIds) {
            setmealDao.addSetGoup(setmealid,checkGroup);

        }

    }
    /*
    * 分页查询
    * */

    @Override
    public PageResult findall(QueryPageBean queryPageBean) {
        //调用工具类 分页查询
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //查询数据
      Page<Setmeal> page = setmealDao.findall(queryPageBean);
        PageResult pageResult = new PageResult();
        pageResult.setTotal(page.getTotal());
        pageResult.setRows(page.getResult());
        return pageResult;
    }
}
